﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Muccu
{
    public class RangeAttribute : ArgumentValidationAttribute
    {
        public RangeAttribute(float min, float max)
        {
            this.Min = min;
            this.Max = max;
            this.ErrorMessage = string.Format("The argument should be in the range {0} and {1}", Min, Max);
        }

        public float Max { get; set; }
        public float Min { get; set; }

        public override bool ServerValidate(object value)
        {
            float val = (float)value;
            return val >= Min && val <= Max;
        }

        public override string ClientValidate
        {
            get
            {
                return @"function(val, messages){
var points = [" + Min + ", " + Max + @"]; 
    var isValid = (val >= points[0] && val <= points[1]);
    if(!isValid)messages.push(""" + ErrorMessage + "\"); return isValid;}";
            }
        }
    }
}
